﻿using System;
using System.Web.Configuration;
using Crawler.Job;
using Quartz;
using Quartz.Impl;


namespace Crawler.Job
{
    public static class Scheduler
    {
        public static IScheduler Schedule()
        {
            //construct a scheduler factory
            ISchedulerFactory schedFact = new StdSchedulerFactory();
            // get a scheduler
            IScheduler sched = schedFact.GetScheduler();
            // construct job info
            JobDetail jobDetail = new JobDetail("ParserJob", null, typeof(ParserJob));
            // fire every hour
            CronTrigger trigger = new CronTrigger("CronTrigger", null, WebConfigurationManager.AppSettings["CronExpression"]);
            // start on the next even hour
            sched.ScheduleJob(jobDetail, trigger);
            return sched;
        }

        public static void Init()
        {
            //construct a scheduler factory
            ISchedulerFactory schedFact = new StdSchedulerFactory();

            // get a scheduler
            IScheduler sched = schedFact.GetScheduler();
            sched.AddGlobalJobListener(new ParserJobListener());
            sched.AddJobListener(new ParserJobListener());
            sched.Start();
        }


        public static  IScheduler ScheduleForOneTime()
        {
             //construct a scheduler factory
            ISchedulerFactory schedFact = new StdSchedulerFactory();
            // get a scheduler
            IScheduler sched = schedFact.GetScheduler();
            sched.Start();
          // construct job info
            JobDetail jobDetail = new JobDetail("ParserJobOneTime", null, typeof(ParserJob));
            // fire every hour
            Trigger trigger = TriggerUtils.MakeImmediateTrigger(0, TimeSpan.Zero);
            // start on the next even hour
            trigger.Name = "ImmediateTrigger";
            sched.ScheduleJob(jobDetail, trigger);
            return sched;
        }

        public static IScheduler ScheduleGrowthForOneTime(int day, int month)
        {
            //construct a scheduler factory
            ISchedulerFactory schedFact = new StdSchedulerFactory();
            // get a scheduler
            IScheduler sched = schedFact.GetScheduler();
            sched.Start();
            // construct job info
            JobDetail jobDetail = new JobDetail("GrowthJobOneTime", null, typeof(GrowthJob));
            jobDetail.JobDataMap.Add("day", day);
            jobDetail.JobDataMap.Add("month", month);
            // fire every hour
            Trigger trigger = TriggerUtils.MakeImmediateTrigger(0, TimeSpan.Zero);
            // start on the next even hour
            trigger.Name = "ImmediateTrigger";
            sched.ScheduleJob(jobDetail, trigger);
            return sched;
        }


    }
}